import type { FormSchemaGetter } from '#/adapter/form';
import type { VxeGridProps } from '#/adapter/vxe-table';

import { DictEnum } from '@vben/constants';
import { useUserStore } from '@vben/stores';

import { customerListByStaffId } from '#/api/hm/customer';
import { userList } from '#/api/system/user';
import { getDictOptions } from '#/utils/dict';
import { renderDict } from '#/utils/render';

const userStore = useUserStore();

export const querySchema: FormSchemaGetter = () => [
  {
    component: 'Select',
    componentProps: {
      options: getDictOptions(DictEnum.TO_DO_TYPE),
    },
    fieldName: 'todoType',
    label: '待办类型',
  },
  {
    component: 'Input',
    fieldName: 'todoDesc',
    label: '待办描述',
  },
  {
    component: 'Select',
    fieldName: 'todoPersonId',
    label: '待办人',
    componentProps: {
      allowClear: true,
      showSearch: true,
      optionFilterProp: 'label',
    },
  },
  {
    component: 'ApiSelect',
    fieldName: 'submitterId',
    label: '提交人',
    componentProps: {
      allowClear: true,
      showSearch: true,
      optionFilterProp: 'label',
      api: async () => {
        const ret = await userList({ pageNum: 1, pageSize: 1000 });
        return ret.rows.map((item) => ({
          label: item.nickName,
          value: item.userId,
        }));
      },
    },
  },
  {
    component: 'ApiSelect',
    fieldName: 'approverId',
    label: '审核人',
    componentProps: {
      allowClear: true,
      showSearch: true,
      optionFilterProp: 'label',
      api: async () => {
        const ret = await userList({ pageNum: 1, pageSize: 1000 });
        return ret.rows.map((item) => ({
          label: item.nickName,
          value: item.userId,
        }));
      },
    },
  },
  {
    component: 'RangePicker',
    componentProps: {
      format: 'YYYY-MM-DD',
      valueFormat: 'YYYY-MM-DD',
    },
    fieldName: 'todoCompletionTime',
    label: '办结时间',
  },
];

export const columns: VxeGridProps['columns'] = [
  { type: 'checkbox', width: 60 },
  {
    title: '事项类型',
    field: 'todoType',
    slots: {
      default: ({ row }) => {
        if (row.todoType) {
          return renderDict(row.todoType, DictEnum.TO_DO_TYPE);
        }
        return '-';
      },
    },
  },
  // {
  //   title: '生成方式',
  //   field: 'createType',
  //   slots: {
  //     default: ({ row }) => {
  //       if (row.createType) {
  //         return renderDict(row.createType, DictEnum.TODO_GENERATION_METHOD);
  //       }
  //       return '-';
  //     },
  //   },
  // },
  {
    title: '待办详情',
    field: 'todoDesc',
    width: 600,
  },
  {
    title: '待办人',
    field: 'todoPersonName',
  },
  {
    title: '提交人',
    field: 'submitterName',
    width: 120,
  },
  {
    title: '审核人',
    field: 'approverName',
    width: 120,
  },
  {
    title: '生成时间',
    field: 'createTime',
    width: 180,
  },
  {
    title: '当前状态',
    field: 'status',
    slots: {
      default: ({ row }) => {
        if (row.status) {
          return renderDict(row.status, DictEnum.TODO_STATUS);
        }
        return '-';
      },
    },
  },
  {
    title: '办结时间',
    field: 'todoCompletionTime',
    width: 180,
  },
  {
    title: '备注',
    field: 'remark',
  },
  {
    field: 'action',
    fixed: 'right',
    slots: { default: 'action' },
    title: '操作',
    width: 240,
  },
];

export const modalSchema: FormSchemaGetter = () => [
  {
    label: '待办id',
    fieldName: 'todoId',
    component: 'Input',
    dependencies: {
      show: () => false,
      triggerFields: [''],
    },
  },
  {
    label: '出库单id',
    fieldName: 'todoOutboundOrder',
    component: 'Input',
    dependencies: {
      show: () => false,
      triggerFields: [''],
    },
  },
  {
    label: '入库单id',
    fieldName: 'todoWarehouseReceipt',
    component: 'Input',
    dependencies: {
      show: () => false,
      triggerFields: [''],
    },
  },
  {
    label: '待办类型',
    fieldName: 'todoType',
    component: 'Select',
    componentProps: {
      options: getDictOptions(DictEnum.TO_DO_TYPE),
    },
    dependencies: {
      disabled: () => true,
      show: () => false,
      triggerFields: ['todoId', ''],
    },
    defaultValue: '0',
    rules: 'selectRequired',
  },
  {
    label: '状态',
    fieldName: 'status',
    component: 'Input',
    rules: 'required',
    dependencies: {
      show: () => false,
      triggerFields: [''],
    },
  },
  {
    label: '生成方式',
    fieldName: 'createType',
    component: 'Select',
    rules: 'required',
    dependencies: {
      show: () => false,
      triggerFields: [''],
    },
    defaultValue: '0',
  },
  {
    label: '回访方式',
    fieldName: 'followType',
    component: 'RadioGroup',
    rules: 'required',
    defaultValue: '1',
    componentProps: {
      buttonStyle: 'solid',
      options: getDictOptions(DictEnum.TODO_FOLLOW_UP_TYPE),
      optionType: 'button',
    },
    dependencies: {
      show: (values) => values?.todoType === '0',
      disabled: (values) => values?.status === '1',
      triggerFields: ['todoType', 'status'],
    },
  },
  {
    label: '回访客户',
    fieldName: 'followCustomerId',
    component: 'Select',
    rules: 'required',
    dependencies: {
      async componentProps() {
        if (userStore.userInfo?.userId) {
          const ret = await customerListByStaffId(userStore.userInfo?.userId);
          const transOptions = ret.map((item) => ({
            label: item.customerName,
            value: item.customerId,
          }));
          const dataSourceOptions = [];
          dataSourceOptions.push(...transOptions);
          return {
            options: dataSourceOptions,
          };
        }
        return {};
      },
      show: (values) => values?.todoType === '0',
      disabled: (values) => values?.todoId != undefined,
      triggerFields: ['todoType', 'todoId'],
    },
  },
  {
    label: '入库商品',
    fieldName: 'todoStockGoodsId',
    component: 'Select',
    rules: 'required',
    dependencies: {
      show: () => false,

      triggerFields: ['todoType'],
    },
  },
  {
    label: '入库商品',
    fieldName: 'todoStockGoodsName',
    component: 'Input',
    rules: 'required',
    dependencies: {
      show: (values) => values?.todoType === '1',
      disabled: (values) => values?.todoType === '1',
      triggerFields: ['todoType'],
    },
  },
  {
    label: '入库数量',
    fieldName: 'todoStockNum',
    component: 'InputNumber',
    rules: 'required',
    dependencies: {
      show: (values) => values?.todoType === '1',
      disabled: (values) => values?.todoType === '1',
      triggerFields: ['todoType'],
    },
  },
  {
    label: '出库店铺',
    fieldName: 'todoOutboundDept',
    component: 'InputNumber',
    rules: 'required',
    dependencies: {
      show: () => false,
      triggerFields: [''],
    },
  },
  {
    label: '入库店铺',
    fieldName: 'todoStockDept',
    component: 'Select',
    defaultValue: userStore.userInfo?.deptId,
    dependencies: {
      show: (values) => values?.todoType === '1',
      disabled: (values) => values?.todoType === '1',
      triggerFields: ['todoType'],
    },
    rules: 'required',
  },
  {
    label: '待办人',
    fieldName: 'todoPersonId',
    component: 'Input',
    rules: 'required',
    dependencies: {
      disabled: (values) => values?.todoId !== undefined,
      show: () => false,
      triggerFields: ['todoId'],
    },
    defaultValue: userStore.userInfo?.userId,
  },
  {
    label: '参会地点',
    fieldName: 'followMeetingLocation',
    component: 'Input',
    rules: 'required',
    dependencies: {
      show: (values) =>
        values?.todoType === '0' && [3, 4].includes(Number(values?.followType)),
      disabled: (values) => values?.status === '1',
      triggerFields: ['todoType', 'status', 'followType'],
    },
  },
  {
    label: '参会时间',
    fieldName: 'followMeetingTime',
    component: 'DatePicker',
    rules: 'required',
    componentProps: {
      showTime: true,
      format: 'YYYY-MM-DD HH:mm:ss',
      valueFormat: 'YYYY-MM-DD HH:mm:ss',
    },
    dependencies: {
      show: (values) =>
        values?.todoType === '0' && [3, 4].includes(Number(values?.followType)),
      disabled: (values) => values?.status === '1',
      triggerFields: ['todoType', 'status', 'followType'],
    },
  },
  {
    label: '会议内容',
    fieldName: 'followMeetingInfo',
    component: 'Textarea',
    rules: 'required',
    componentProps: {
      showCount: true,
      maxlength: 200,
      rows: 5,
      placeholder: '请输入会议内容',
    },
    dependencies: {
      show: (values) =>
        values?.todoType === '0' && [3, 4].includes(Number(values?.followType)),
      disabled: (values) => values?.status === '1',
      triggerFields: ['todoType', 'status', 'followType'],
    },
  },
  {
    label: '领取礼品',
    fieldName: 'followMeetingGifts',
    component: 'Input',
    dependencies: {
      show: (values) => values?.todoType === '0',
      disabled: (values) => values?.status === '1',
      triggerFields: ['todoType', 'status', 'followType'],
    },
  },
  {
    label: '满意度',
    fieldName: 'followMeetingLevel',
    component: 'RadioGroup',
    rules: 'required',
    componentProps: {
      buttonType: 'solid',
      options: getDictOptions(DictEnum.CUSTOMER_SATISFACTION),
      optionType: 'button',
    },
    dependencies: {
      show: (values) => values?.todoType === '0',
      disabled: (values) => values?.status === '1',
      triggerFields: ['todoType', 'status'],
    },
  },
  // {
  //   label: '证明文件',
  //   fieldName: 'todoFiles',
  //   component: '',
  //   /**
  //    * 注意这里获取为数组 需要自行定义回显/提交
  //    * 文件上传还在demo阶段 可能有重大改动!
  //    */
  //   componentProps: {
  //     accept: ['png', 'jpg', 'jpeg', 'xlsx', 'pdf'], // 不支持type/*的写法 建议使用拓展名(不带.)
  //     maxNumber: 3, // 最大上传文件数
  //     resultField: 'url', // 上传成功后返回的字段名 默认url 可选['ossId', 'url', 'fileName']
  //   },
  //   dependencies: {
  //     if: (values) => values?.todoType === '0' || values?.status !== undefined,
  //     disabled: (values) => values?.status === '1',
  //     triggerFields: ['status', 'todoType'],
  //   },
  //   rules: 'required',
  // },

  {
    label: '处理结果',
    fieldName: 'todoResult',
    component: 'Input',
    rules: 'required',
    dependencies: {
      if: (values) => values?.todoType === '0' || values?.status !== undefined,
      disabled: (values) => values?.status === '1',
      triggerFields: ['status', 'todoType'],
    },
  },
  {
    label: '处理时间',
    fieldName: 'todoCompletionTime',
    component: 'DatePicker',
    componentProps: {
      format: 'YYYY-MM-DD',
      valueFormat: 'YYYY-MM-DD',
    },
    rules: 'required',
    dependencies: {
      if: (values) => values?.todoType === '0' || values?.status !== undefined,
      disabled: (values) => values?.status === '1',
      triggerFields: ['status'],
    },
  },

  {
    label: '事项描述',
    fieldName: 'todoDesc',
    component: 'Textarea',
    rules: 'required',
    dependencies: {
      disabled: (values) => values?.todoId !== undefined,
      show: (values) => [1, 2, 5].includes(Number(values?.followType)),
      triggerFields: ['todoId', 'followType'],
    },
    componentProps: {
      showCount: true,
      maxlength: 200,
      rows: 7,
      placeholder:
        `目的:\n` +
        `例如新老员工交接;顺路看望;中秋慰问;生日祝贺;病后看后等等，\n` +
        `购买了什么产品，因为什么购买的: 例如购买了辅酶 Q10 是因为心脏不舒服.\n` +
        `服用情况: 例如服用量多少，每天服用几次等等\n` +
        `服用效果:例如改善了什么症状等等\n` +
        `其他事项:例如服用其他家产品情况等`,
    },
  },
  {
    label: '证明材料',
    fieldName: 'todoFiles',
    component: 'ImageUpload',
    componentProps: {
      // accept: ['jpg'], // 不支持type/*的写法 支持拓展名(不带.) 文件头(image/png这种)
      maxNumber: 3, // 最大上传文件数 默认为1 为1会绑定为string而非string[]类型
      resultField: 'url', // 上传成功后返回的字段名 默认url 可选['ossId', 'url', 'fileName']
    },
    rules: 'required',
    dependencies: {
      if: (values) => values?.todoType === '0' || values?.status !== undefined,
      triggerFields: ['status', 'todoType'],
    },
  },
  {
    label: '客户反馈',
    fieldName: 'remark',
    component: 'Textarea',
    componentProps: {
      showCount: true,
      maxlength: 200,
      rows: 5,
      placeholder: '请输入客户反馈',
    },
    dependencies: {
      show: (values) => values?.todoType === '0',
      disabled: (values) => values?.status === '1',
      triggerFields: ['status', 'todoType'],
    },
  },
  {
    label: '退货id',
    fieldName: 'returnOrderId',
    component: 'input',
    dependencies: {
      show: () => false,
      triggerFields: ['status', 'todoType'],
    },
  },
];
